package cn.duli.myvideo.controller;

import cn.duli.myvideo.pojo.Tv;
import cn.duli.myvideo.service.TvService;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/tv")
public class TvController {

    @Resource
    private TvService tvService;


    /**
     * 根据电视剧名称模糊分页查询电视剧列表
     * @return
     */
    @GetMapping("/queryLike")
    public PageInfo<Tv> queryLike(
            @RequestParam(value = "tvName") String tvName,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Tv> pageInfo = new PageInfo<>(tvService.queryLike(tvName));
        return pageInfo;
    }


    /**
     * 查询所有电视剧
     * @param pageNo 分页的页码
     * @param pageSize  每页的条数
     * @return
     */
    @GetMapping("/findAll")
    public PageInfo<Tv> findAll(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Tv> pageInfo = new PageInfo<>(tvService.findAll());
        return pageInfo;
    }


    /**
     * 根据id查找电视剧
     * @param tvId
     * @return
     */
    @GetMapping("/findById/{tvId}")
    public Tv findById(@PathVariable("tvId") Integer tvId){
        return tvService.findById(tvId);
    }

    /**
     * 增加电视剧
     * @param tv
     */
    @PostMapping("/save")
    public void save(@RequestBody Tv tv){
        tvService.save(tv);
    }

    /**
     * 修改电视剧
     * @param tv
     */
    @RequestMapping("/update")
    public void update(@RequestBody Tv tv){
        tvService.update(tv);
    }

    /**
     * 根据id删除电视剧
     * @param tvId
     */
    @RequestMapping("/deleteById/{tvId}")
    public void deleteById(@PathVariable("tvId") Integer tvId){
        tvService.deleteById(tvId);
    }
}
